package com.swt.gap.common.graph;

import com.swt.gap.common.constant.RedisConstant;
import com.swt.gap.common.graph.Neo4jTools;
import com.swt.gap.util.RedisUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;

import java.util.Set;

/**
 * @author sx
 */
@Repository
public class GraphService {

    private final Neo4jTools neo4jTools;
    private final RedisUtil redisUtil;

    public GraphService(Neo4jTools neo4jTools, RedisUtil redisUtil) {
        this.neo4jTools = neo4jTools;
        this.redisUtil = redisUtil;
    }

    @Async
    public Boolean deleteGraphByTaskId(Integer taskId) {
        String cypher = "match (n)-[r]-() where n.taskId=" + taskId + "   delete r,n";
        neo4jTools.executeCypherNoReturn(cypher);

        Set<String> relKeys = redisUtil.keys(RedisConstant.KEY_PREFIX_RELATIONSHIP + taskId + "_*");
        redisUtil.del(relKeys);
        redisUtil.del(RedisConstant.KEY_PREFIX_TASK + taskId);
        return true;
    }
}



